↜ Back to index Introduction to Numerical Analysis 1
Solution Lecture a2
Exercise 3
implicit none
integer a, i
print *, 'Enter a positive integer'
read *, a
do i = 1, 10
if (mod(a, 2) == 0) then
= a / 2
a else
= 3 * a + 1
a endif
print *, a
enddo
end
Exercise 4
implicit none
integer a, i, m
print *, 'Enter a positive integer'
read *, a
= 0 ! elements of Collatz sequence are positive so 0 is OK
m do i = 1, 10
if (mod(a, 2) == 0) then
= a / 2
a else
= 3 * a + 1
a endif
= max(m, a)
m enddo
print *, m
end